package com.brilliance.project.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.brilliance.project.common.Constants;
import com.brilliance.project.dao.IBaseDao;
import com.brilliance.project.webapp.form.BaseForm;

public class BaseDaoImpl extends SqlSessionDaoSupport implements IBaseDao {
	public Logger log = Logger.getLogger(this.getClass());

	/**
	 * 分页查询
	 * 
	 * @param statementName
	 * @param baseForm
	 * @return
	 * @throws Exception
	 * @see com.project.dao.IBaseDao#queryWithPg(java.lang.String,
	 *      com.project.webapp.form.BaseForm)
	 */
	public List<BaseForm> queryWithPg(String statementName, BaseForm baseForm) {
		Object count = this.getSqlSession().selectOne(
				this.getCountQuery(statementName), baseForm);
		// 资料总笔数
		String totalCount = count == null ? "0" : count.toString();
		int totalCountInt = Integer.parseInt(totalCount);

		// 每页显示笔数
		int countPerPage = baseForm.getCountPerPage() == 0 ? Constants.DEFAULT_MAX_PAGE_RECORD
				: baseForm.getCountPerPage();

		// 资料总页数
		String total = "";
		Integer totalInt = null;
		if (totalCountInt != 0) {
			if (totalCountInt % countPerPage == 0) {
				totalInt = totalCountInt / countPerPage;
			} else {
				totalInt = totalCountInt / countPerPage + 1;
			}
			total = totalInt + "";
		} else {
			total = "0";
		}

		// 计算从第几笔数据开始
		int startCount = 0;
		String curPageNo = "0";
		if ("N".equals(baseForm.getIsFirstQuery())) {
			curPageNo = baseForm.getCurPageNo() == null ? "0" : baseForm
					.getCurPageNo();
		} else {
			baseForm.setCurPageNo("1");
		}
		int curPageNoInt = Integer.parseInt(curPageNo);
		if (curPageNoInt != 0) {
			startCount = (curPageNoInt - 1) * countPerPage;
		}
		baseForm.setTotalRecord(totalCount);
		baseForm.setTotal(total);
		baseForm.setCountPerPage(countPerPage);
		baseForm.setStartCount(startCount);

		List list = this.getSqlSession().selectList(statementName, baseForm);
		if (list == null || list.size() == 0) {
			baseForm.setCurPageNo("0");
		}
		return list;
	}

	public void setPageInfo(Integer totalInteger, BaseForm baseForm) {
		// 资料总笔数
		int totalCountInt = totalInteger.intValue();
		baseForm.setTotalRecord(""+totalCountInt);
		// 每页显示笔数
		int countPerPage = baseForm.getCountPerPage() == 0 ? Constants.DEFAULT_MAX_PAGE_RECORD
				: baseForm.getCountPerPage();

		// 开始笔数
		baseForm.setStartCount((Integer.parseInt(baseForm.getCurPageNo())-1)*countPerPage);
		
	}

	private String getCountQuery(String queryName) {
		int i = queryName.indexOf(".");
		return queryName.substring(0, i) + ".count"
				+ Character.toUpperCase(queryName.charAt(i + 1))
				+ queryName.substring(i + 2);
	}

	public static Connection getConnection(String jndiName)
			throws NamingException, SQLException {
		if (Constants.DATA_SOURCE == 0) {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/" + jndiName);
			return ds.getConnection();
		} else {
			try {
				Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
				String url = "jdbc:sybase:Tds:10.69.50.11:4100/jf";
				String userName = "sa";
				String password = "";
				return DriverManager.getConnection(url, userName, password);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		BaseDaoImpl.getConnection("");
	}
}
